package com.xique.blelock.Adapter;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.Adapter;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.qmuiteam.qmui.widget.QMUILoadingView;
import com.xique.blelock.Interface.OnItemClickListener;
import com.xique.blelock.R;
import com.xique.blelock.model.FingerInfo;
import com.xique.blelock.model.LockAllinfo;
import com.xique.blelock.model.UserOfLockinfo;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/12/7.
 */

public class FingerAdapter extends Adapter<FingerAdapter.ViewHolder> {
    private List<FingerInfo> mDatas = new ArrayList<>();
    private OnItemClickListener onItemClickListener;
    private LockAllinfo lockAllinfo;

    public FingerAdapter(List<FingerInfo> bleDevices, LockAllinfo lockAllinfo) {
        this.mDatas.addAll(bleDevices);
        this.lockAllinfo = lockAllinfo;
    }

    public void setDeviceList(List<FingerInfo> deviceList, LockAllinfo lockAllinfo){
        this.mDatas.clear();
        this.mDatas.addAll(deviceList);
        this.lockAllinfo = lockAllinfo;
    }



    @NonNull
    @Override
    public FingerAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //LayoutInflater.from指定写法
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_foundble, parent, false);
        ViewHolder viewHolder = new ViewHolder(view);
        viewHolder.rootlayout = view.findViewById(R.id.rootlayout);
        viewHolder.name = view.findViewById(R.id.devname);
        viewHolder.lefticon = view.findViewById(R.id.icon);
        viewHolder.righticon = view.findViewById(R.id.righticon);
        return viewHolder;

    }

    @Override
    public void onBindViewHolder(@NonNull final FingerAdapter.ViewHolder holder, final int position) {
        if(TextUtils.isEmpty(mDatas.get(position).getNickname())){
            holder.name.setText("未知用户");
        }else
        {
            holder.name.setText(mDatas.get(position).getNickname());
        }
        holder.lefticon.setImageResource(R.drawable.u184);

        if(mDatas.get(position).getOrder() == FingerInfo.RootRole){//需要为1是超级管理员
            holder.righticon.setVisibility(View.VISIBLE);
            holder.righticon.setImageResource(R.drawable.admin);
        }else if(mDatas.get(position).getOrder() == lockAllinfo.getSecondorder()){//副管理员
            holder.righticon.setVisibility(View.VISIBLE);
            holder.righticon.setImageResource(R.drawable.admin2);
        }else{//普通用户指纹
            holder.righticon.setVisibility(View.GONE);
        }

        holder.rootlayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(onItemClickListener != null){
                    onItemClickListener.onItemClick(mDatas.get(position), position);
                }
            }
        });

    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        public ViewHolder(View itemView) {
            super(itemView);
        }


        View rootlayout;
        ImageView lefticon;
        TextView name;
        ImageView righticon;
        QMUILoadingView loadingView;

    }
}
